package com.wxd.modules.sys.controller;

import com.wxd.annotation.SysLog;
import com.wxd.modules.sys.entity.SysRoleEntity;
import com.wxd.modules.sys.entity.SysUserEntity;
import com.wxd.modules.sys.service.SysRoleService;
import com.wxd.modules.sys.service.SysUserRoleService;
import com.wxd.modules.sys.service.SysUserService;
import com.wxd.modules.sys.shiro.ShiroUtils;
import com.wxd.utils.PageUtils;
import com.wxd.utils.R;
import com.wxd.validator.Assert;
import com.wxd.validator.ValidatorUtils;
import com.wxd.validator.group.AddGroup;
import com.wxd.validator.group.UpdateGroup;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 系统用户
 */
@RestController
@RequestMapping("sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleService sysRoleService;


    /**
     *所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam Map<String,Object> params)
    {
        PageUtils page = sysUserService.queryPage(params);
        return R.ok().put("page",page);
    }
    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public R info(){
       return R.ok().put("user", getUser());
    }
    /**
     * 获取登录用户的角色信息
     */
    @RequestMapping("/loginUserRole")
    public R loginUserRole()
    {
        List<SysRoleEntity> roleList=new ArrayList<>();
        //获取用户的角色列表
        if(getUserId()!=1)
        {
            List<Long> roleIdList = sysUserRoleService.queryRoleIdList(getUserId());

            for(Long id:roleIdList)
            {
                SysRoleEntity role = sysRoleService.getById(id);
                roleList.add(role);
            }
        }
        else{
            SysRoleEntity role=new SysRoleEntity();
            role.setRoleName("系统管理员");
            roleList.add(role);
        }

        return R.ok().put("userRole",roleList);
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @RequestMapping("/password")
    public R password(String password, String newPassword){
        Assert.isBlank(newPassword, "新密码不为能空");

        //原密码
        password = ShiroUtils.sha256(password, getUser().getSalt());
        //新密码
        newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if(!flag){
            return R.error("原密码不正确");
        }
        return R.ok();
    }


    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId")  Long userId)
    {
        SysUserEntity user = sysUserService.getById(userId);
        //获取用户的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);
        return R.ok().put("user",user);
    }
    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUserEntity user)
    {
        ValidatorUtils.validateEntity(user, AddGroup.class);
        sysUserService.saveUser(user);
       return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user)
    {
         ValidatorUtils.validateEntity(user, UpdateGroup.class);
         sysUserService.update(user);
         return R.ok();
    }
    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds)
    {
        if(ArrayUtils.contains(userIds,1L))
        {
            return R.error("系统管理员不能删除");
        }
        if(ArrayUtils.contains(userIds,getUserId()))
        {
            return R.error("当前用户不能删除");
        }
        sysUserService.removeByIds(Arrays.asList(userIds));
        return R.ok();
    }
}
